﻿@page
@model ToolGood.SqlOnline.Pages.Admins.AdminGroup.EditModel
@{
    Layout = "~/Pages/Shared/_Layout_Edit.cshtml";
}
@section css{
    <style>
        .layui-table td, .layui-table th { padding: 3px; }
        .layui-form-item .layui-form-checkbox[lay-skin=primary] { margin-top: 5px; margin-bottom: 5px; }
    </style>
}

<div style="overflow-x:hidden;background-color:#f3f3f4;">
    <form id="edit-form">
        <input type="hidden" name="id" value="@Model.AdminGroup.Id" />
        @Html.AntiForgeryToken()
        <div class="layui-form" lay-filter="form" style="padding: 20px 30px 0 0;">
            <div class="layui-form-item">
                <label class="layui-form-label">角色名<span class="red"> *</span></label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" id="txt_name" name="name" value="@Model.AdminGroup.Name" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">描述</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" id="txt_Comment" name="Comment" value="@Model.AdminGroup.Comment" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" id="txt_OrderNum" name="OrderNum" value="@Model.AdminGroup.OrderNum" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">权限</label>
                <div class="layui-input-block">
                    <table class="layui-table" lay-skin="line">
                        <thead style="text-align:center;font-weight:700;">
                            <tr>
                                <td width="160">菜单名称</td>
                                <td>操作</td>
                                <td width="80">
                                    <input type="checkbox" lay-skin="primary" data-type="all" lay-filter="all" data-id="0" title="全选" />
                                </td>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.Menus) {
                                var length = item.ParentIds.Length - item.ParentIds.Replace("-", "").Length - 2;
                                <tr style="@(length.ToHtmlWhenIs(0," background-color:#77CAF0",""))">
                                    <td style="font-weight:700;">
                                        @for (int i = 0; i < length; i++) {
                                            <span>&nbsp;&nbsp;&nbsp;</span>
                                        }
                                        @item.MenuName
                                    </td>
                                    <td>
                                        @foreach (var action in item.Buttons.Split('|')) {
                                            <input type="hidden" name="Menus[@(item.Id)-@action].MenuId" value="@item.Id" />
                                            <input type="hidden" name="Menus[@(item.Id)-@action].MenuCode" value="@item.MenuCode" />
                                            <input type="hidden" name="Menus[@(item.Id)-@action].ButtonCode" value="@action" />
                                            <input type="hidden" name="Menus[@(item.Id)-@action].ButtonId" value="@Model.GetButtonId(action)" />
                                            <input type="checkbox" lay-skin="primary" title="@Model.GetButtonName(action)" name="Menus[@(item.Id)-@action].Pass" value="true" data-id="@(item.Id)" data-pid="@(item.ParentIds)" @Model.IsChecked(item.Id, action) />
                                        }
                                    </td>
                                    <td style="text-align:center">
                                        <input type="checkbox" lay-skin="primary" data-id="@(item.Id)" data-pid="@(item.ParentIds)" data-type="items" lay-filter="items" title="全选" />
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>


                </div>
            </div>

            <div class="layui-form-item" style="text-align:center;padding-left: 30px;">
                <button type="button" class="layui-btn layui-btn-normal" onclick="save(this);"> &nbsp;&nbsp; 提 交 &nbsp;&nbsp; </button>
                <button type="button" class="layui-btn layui-btn-primary" onclick="closeWindow();"> &nbsp;&nbsp; 关 闭 &nbsp;&nbsp; </button>
            </div>
        </div>
    </form>
</div>
@section js{
    <script>
        $(function () {
            var form = layui.form;

            form.on('checkbox(items)', function (data) {
                var $val = data.elem.checked;
                var $this = data.elem;
                var id = $($this).attr("data-id");
                var pid = $($this).attr("data-pid");

                var data = {};
                $('input:checkbox[data-id="' + id + '"]').each(function () {
                    var name = $(this).attr("name");
                    if (name != "") { data[name] = $val; }
                });
                $('input:checkbox').each(function () {
                    var inputPid = $(this).attr("data-pid");
                    if (inputPid != undefined && inputPid.startsWith(pid + id + "-")) {
                        var name = $(this).attr("name");
                        if (name != "") { data[name] = $val; }
                    }
                })
                form.val("form", data);
            });

            form.on('checkbox(all)', function (data) {
                var $val = data.elem.checked;
                var data = {};
                $('input:checkbox').each(function () {
                    var name = $(this).attr("name");
                    if (name != "") { data[name] = $val; }
                });
                form.val("form", data);
            });
        });
    </script>
    <script>
        function save(obj) {
            if ($("#txt_name").val().length < 2) { layer.alert("请输入角色名"); $("#txt_name").focus(); return; }
            if (/^\d+$/.test($("#txt_OrderNum").val()) == false) { layer.alert("请输入正确的排序编号"); $("#txt_OrderNum").focus(); return; }
            $(obj).prop("disabled", true);
            var url = "./Ajax/EditAdminGroup";
            var data = $("#edit-form").serializeJson();
            $.adminModePost(url, data, function () {
                closeWindowAndReload();
            }, function (data) {
                $(obj).prop("disabled", false);
                if (data.message) {
                    layer.msg(data.message);
                } else {
                    layer.msg("出错了");
                }
            });
        }
    </script>
}
